﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.IO;
using System.Data.SQLite;
using System.Security.Cryptography.X509Certificates;
using System.Data.Common;
using System.Net.WebSockets;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Net;
using System.Timers;

namespace SocketClient
{
    class InitSocket
    {
        private ConnServer conn;
        private String webSocketLinkUrl = "";
        readonly System.Timers.Timer _timer;
        //readonly System.Timers.Timer _loginTimer;
        readonly System.Timers.Timer _uploadTimer;
        private int timerCount = 0;
        public InitSocket()
        {
            GValues.scoketConnectKey = 0;
            NetApi netapi = new NetApi();
            netapi.InitDomainConfig();
            netapi.getUserInfo();
            netapi.localLogin(); //启动服务登入系统
            webSocketLinkUrl = GValues.domainLink.Replace("http", "ws").Replace("https", "wss") + "websocket/" + GValues.tenant + "-" + GValues.account + "-print";
            _timer = new System.Timers.Timer(3600000) //6小时启动一次登入系统
            {
                AutoReset = true,
                Enabled = true
            };
            //_loginTimer = new System.Timers.Timer(30000000)
            //{
            //    AutoReset = true,
            //    Enabled = true
            //};
            ////upload timer
            _uploadTimer = new System.Timers.Timer(GValues.uploadTimeCount)
            {
                AutoReset = true,
                Enabled = true
            };
            _timer.Elapsed += delegate(object sender, ElapsedEventArgs e)
            {
                //LogHelper.WriteLog("Socket状态！" + GValues.scoketConnectKey);
                //LogHelper.WriteLog("重启Socket并登入系统！");
                //Start();
                LogHelper.WriteLog("重启登入系统！");
                netapi.localLogin(); //启动服务登入系统
            };
            //_loginTimer.Elapsed += delegate(object sender, ElapsedEventArgs e)
            //{
            //    LogHelper.WriteLog("重启登入系统！");
            //    netapi.localLogin(); //启动服务登入系统
            //};


            _uploadTimer.Elapsed += delegate(object sender, ElapsedEventArgs e)
            {
                timerCount++;
                int targetMinute = 50; // 假设我们要判断当前时间是否在30分钟
                DateTime currentTime = DateTime.Now;
                UploadPdf uppdf = new UploadPdf();
                if (GValues.uploadComplete || currentTime.Minute >= targetMinute || timerCount == 2)
                {
                    LogHelper.WriteLog("进入循环：uploadComplete = " + GValues.uploadComplete);
                    timerCount = 0;
                    uppdf.uploadPdf();
                }
            };
        }

        public void Stop()
        {
            this.conn.CloseConnect();
            Application.Exit();
            //Process.GetCurrentProcess().Kill();
        }
        public void Start()
        {
            Thread thread = new Thread(new ParameterizedThreadStart(this.Thread_conn));
            thread.IsBackground = true;
            thread.Start(new string[]
            {
                webSocketLinkUrl,
                "0",
                "WebSocket"
            });
        }
        private void Thread_conn(object obj)
        {
            string[] array = (string[])obj;
            this.conn = new ConnServer(array[0], int.Parse(array[1]), array[2]);
            this.conn.Connect();
        }
    }
}
